<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工单号" prop="flowNum">
        <el-input
          v-model="queryParams.flowNum"
          placeholder="请输入工单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产编码" prop="assetNum">
        <el-input
          v-model="queryParams.assetNum"
          placeholder="请输入资产编码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修时间" prop="startTime">
        <el-date-picker
          v-model="daterangeMaintainTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="维修结果" prop="result">
        <el-select v-model="queryParams.result" placeholder="请选择维修结果" style="width: 160px" clearable>
          <el-option
            v-for="dict in maintain_result"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['asset:maintain:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单号" align="center" prop="flowNum" />
      <el-table-column label="资产分类" align="center" prop="cateName" />
      <el-table-column label="资产编码" align="center" prop="assetNum" />
      <el-table-column label="维护原由" align="center" prop="reason" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180" />
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180" />
      <el-table-column label="维修费用" align="center" prop="money" />
      <el-table-column label="维修说明" align="center" prop="comment" />
      <el-table-column label="维修图片" align="center" prop="imgs" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.imgs" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="维修结果" align="center" prop="result">
        <template #default="scope">
          <dict-tag :options="maintain_result" :value="scope.row.result"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['asset:maintain:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产维修对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="maintainRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="维修工单" prop="flowNum">
          <el-input v-model="form.flowNum" placeholder="请输入工单编号" readonly/>
        </el-form-item>
        <el-form-item label="资产编码" prop="assetNum">
          <el-input v-model="form.assetNum" placeholder="请输入资产编码" readonly/>
        </el-form-item>
        <el-form-item label="维护原由" prop="reason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入维护原由" readonly/>
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成时间" prop="finishTime">
          <el-date-picker clearable
            v-model="form.finishTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修费用" prop="money">
          <el-input v-model="form.money" placeholder="请输入维修费用" />
        </el-form-item>
        <el-form-item label="维修说明" prop="comment" >
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="维修图片" prop="imgs">
          <image-upload v-model="form.imgs"/>
        </el-form-item>
        <el-form-item label="维修结果" prop="result">
          <el-radio-group v-model="form.result">
            <el-radio
              v-for="dict in maintain_result"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Maintain">
import { listMyMaintain, getMaintain, delMaintain, addMaintain, updateMaintain } from "@/api/asset/maintain";
import { selectFlow } from "@/api/asset/flow";
import { queryDs } from "@/api/dev/dataset";

const { proxy } = getCurrentInstance();
const { maintain_result } = proxy.useDict('maintain_result');

const maintainList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeMaintainTime = ref([]);
const flowOptions = ref([]);
const userOptions = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 't.id',
    isAsc: 'DESC',
    flowId: null,
    flowNum: null,
    assetId: null,
    assetNum: null,
    userId: null,
    startTime: null,
    finishTime: null,
    imgs: null,
    result: null,
  },
  rules: {
    flowId: [
      { required: true, message: "工单ID不能为空", trigger: "blur" }
    ],
    assetId: [
      { required: true, message: "资产ID不能为空", trigger: "blur" }
    ],
    assetNum: [
      { required: true, message: "资产编码不能为空", trigger: "blur" }
    ],
    reason: [
      { required: true, message: "维护原由不能为空", trigger: "blur" }
    ],
    createBy: [
      { required: true, message: "创建人不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询资产维修列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeMaintainTime && '' != daterangeMaintainTime) {
    queryParams.value.params["beginTime"] = daterangeMaintainTime.value[0];
    queryParams.value.params["endTime"] = daterangeMaintainTime.value[1];
  }
  listMyMaintain(queryParams.value).then(response => {
    maintainList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    flowId: null,
    flowNum: null,
    assetId: null,
    assetNum: null,
    reason: null,
    userId: null,
    startTime: null,
    finishTime: null,
    money: null,
    comment: null,
    imgs: null,
    result: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("maintainRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加资产维修";
  //加载工单
  selectFlow({typeCode:'maintain'}).then(response=>{
    flowOptions.value = response.data;
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMaintain(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改资产维修";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["maintainRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMaintain(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMaintain(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除资产维修编号为"' + _ids + '"的数据项？').then(function() {
    return delMaintain(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('asset/maintain/export', {
    ...queryParams.value
  }, `maintain_${new Date().getTime()}.xlsx`)
}


/** 复制选中工单的设备信息 */
function changeFlow(){
  for(var i=0;i<flowOptions.value.length;i++){
    if(flowOptions.value[i].id == form.value.flowId){
      form.value.flowNum = flowOptions.value[i].num;
      form.value.assetNum = flowOptions.value[i].assetNum;
      form.value.assetId = flowOptions.value[i].assetId;
      let reason = flowOptions.value[i].reason;
      if(flowOptions.value[i].content){
        reason += flowOptions.value[i].content;
      }
      form.value.reason = reason;
      break;
    }
  }
}

getList();


queryDs({"code":"sys_user_select"}).then(response=>{
    userOptions.value = response.data;
});

</script>
